package com.xiaohua.api.config;

import com.aliyun.docmind_api20220711.Client;
import com.aliyun.teaopenapi.models.Config;
import com.xiaohua.api.common.properties.AliYunProperties;
import com.xiaohua.api.common.properties.TextInProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云客户端配置类
 *
 * @author 小花
 * @date 2025-03-29
 */
@Configuration
public class AliYunClientConfig {
    
    private static final Logger log = LoggerFactory.getLogger(AliYunClientConfig.class);
    
    @Autowired
    private AliYunProperties aliYunProperties;
    
    @Autowired
    private TextInProperties textInProperties;
    
    /**
     * 创建阿里云文档转换客户端
     *
     * @return 阿里云文档转换客户端
     */
    @Bean
    public Client docmindClient() {
        try {
            // 获取AK/SK
            String accessKeyId = aliYunProperties.getAccessKeyId();
            String accessKeySecret = aliYunProperties.getAccessKeySecret();
            
            if (accessKeyId == null || accessKeySecret == null) {
                log.error("阿里云API凭证未正确加载，AccessKeyId或AccessKeySecret为空");
                throw new RuntimeException("阿里云API凭证未正确加载，请检查数据库配置");
            }
            
            log.info("初始化阿里云文档转换客户端Bean，使用AK：{}", accessKeyId.substring(0, Math.min(4, accessKeyId.length())) + "********");
            
            // 创建配置
            Config config = new Config();
            config.setAccessKeyId(accessKeyId);
            config.setAccessKeySecret(accessKeySecret);
            config.setEndpoint("docmind-api.cn-hangzhou.aliyuncs.com");
            
            // 使用配置创建客户端
            Client client = new Client(config);
            log.info("阿里云文档转换客户端Bean创建成功");
            
            // 同时检查TextIn API凭证是否加载
            checkTextInCredentials();
            
            return client;
        } catch (Exception e) {
            log.error("创建阿里云文档转换客户端Bean失败：{}", e.getMessage(), e);
            throw new RuntimeException("创建阿里云文档转换客户端Bean失败：" + e.getMessage(), e);
        }
    }
    
    /**
     * 检查TextIn API凭证是否正确加载
     */
    private void checkTextInCredentials() {
        try {
            String appId = textInProperties.getAppId();
            String secretCode = textInProperties.getSecretCode();
            
            if (appId == null || secretCode == null) {
                log.error("TextIn API凭证未正确加载，AppId或SecretCode为空");
            } else {
                log.info("TextIn API凭证已加载，AppId：{}", maskString(appId));
            }
        } catch (Exception e) {
            log.error("检查TextIn API凭证失败：{}", e.getMessage(), e);
        }
    }
    
    /**
     * 掩码显示字符串，仅显示前几个字符
     */
    private String maskString(String input) {
        if (input == null || input.length() <= 4) {
            return input;
        }
        return input.substring(0, Math.min(4, input.length())) + "********";
    }
} 